 /**
 * Description: 物资入库记录
 * @author:Hunter
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-04-08   Hunter       1.0.0     Initial
 */
package com.metro.paperless.app.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.JsonResult;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.paperless.app.beans.vo.AppInDetail;
import com.metro.paperless.app.beans.vo.AppInDetailForm;
import com.metro.paperless.app.beans.vo.AppOutDetail;
import com.metro.paperless.app.mapper.IAppInDetailMapper;
import com.metro.paperless.app.mapper.IAppOutDetailMapper;
import com.metro.paperless.biz.beans.vo.BizMaterialsManage;
import com.metro.paperless.biz.mapper.IBizMaterialsManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.paperless.app.mapper.IAppMaterialsInMapper;
import com.metro.paperless.app.service.inf.IAppMaterialsInService;
import com.metro.paperless.app.beans.vo.AppMaterialsIn;

import java.util.Date;
import java.util.List;

 /**
 * 物资入库记录<br>
 *
 * @author:Hunter
 * @since:1.0.0
 * @version [1.0.0, 2018-04-08]
 */
@Service("appmaterialsinService")
public class AppMaterialsInService extends BaseServiceSupport<AppMaterialsIn> implements IAppMaterialsInService{
    //物资入库记录Mapper
    @Autowired
    private IAppMaterialsInMapper appmaterialsinMapper;
     @Autowired
     private IAppInDetailMapper inDetailMapper;
     @Autowired
     private IBizMaterialsManageMapper materialsManageMapper;

    @Override
    public IMapper getMapper() {
        return appmaterialsinMapper;
    }

    @Override
    public IResult save(AppMaterialsIn entity) {
        try {
            appmaterialsinMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增物资入库记录失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

     @Override
     public IResult update(AppMaterialsIn appMaterialsIn) {
         return null;
     }

     @Override
     public JsonResult save(AppInDetailForm inDetailForm, AppMaterialsIn entity) {
         try {
             JsonResult jr = new JsonResult();
             entity.setActionTime(new Date());
             entity.setInStatus("1");
             appmaterialsinMapper.save(entity);
             Long inId = entity.getId();
             for (AppInDetail itementity : inDetailForm.getItemInEntity()) {
                 itementity.setInBills(inId);
                 itementity.setInStatus(0);
                 inDetailMapper.save(itementity);
             }
             jr.setData(inId);
             jr.setResult(ResultCode.SUCCESS);
             return jr;
         } catch (Exception e) {
             log.error("新增物资出库记录失败", e);
             throw new ServiceException(ResultCode.ERROR);
         }
     }

     @Override
     public IResult update(AppInDetailForm itemEntity, AppMaterialsIn entity) {
         try {
             appmaterialsinMapper.updateNotEmpty(entity);
             Long inId = entity.getId();
             inDetailMapper.deleteByBillIds(inId.toString());//现有的所有记录准备重新添加

             for (AppInDetail itementity : itemEntity.getItemInEntity()) {
                 itementity.setInBills(inId);
                 itementity.setInStatus(0);
                 inDetailMapper.save(itementity);
             }
             return ResultCode.SUCCESS;
         } catch (Exception e) {
             log.error("修改物资入库记录失败", e);
             throw new ServiceException(ResultCode.ERROR);
         }
     }

     @Override
    public IResult update(AppMaterialsIn entity,String action) {
        try {
            appmaterialsinMapper.updateNotEmpty(entity);
            List<AppInDetail> inDetailList = inDetailMapper.queryListByInId(entity.getId());
            for (AppInDetail itementity : inDetailList) {
                if("commint".equals(action)){
                    itementity.setInStatus(1);
                }else{
                    itementity.setInStatus(3);
                }
                inDetailMapper.updateNotEmpty(itementity);
                Long matId = itementity.getMaterialsId();
                BizMaterialsManage materialsManage = materialsManageMapper.getById(matId);
                int stockNum;
                int enablestockNum;

                if("commint".equals(action)){
                     stockNum = materialsManage.getStockNum()+itementity.getInNum();
                     enablestockNum = materialsManage.getEnableStockNum()+itementity.getInNum();
                }else{
                     stockNum = materialsManage.getStockNum()-itementity.getInNum();
                     enablestockNum = materialsManage.getEnableStockNum()-itementity.getInNum();
                }
                materialsManage.setStockNum(stockNum);
                materialsManage.setEnableStockNum(enablestockNum);
                materialsManageMapper.updateNotEmpty(materialsManage);
            }
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改物资入库记录失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }




    @Override
    public IResult delete(Long id) {
        try {
            appmaterialsinMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            appmaterialsinMapper.deleteByIds(ids);//删除记录表数据
            inDetailMapper.deleteByBillIds(ids);//删除明细表数据
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

 }